package com.zengdada.authority.annotation;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zengdada.authority.model.SysFunkLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class JobSysLogAop extends BaseLogAop<JobSysLog> {

    //这个里面需要写自定义注解的全限定名（包名+类名）
    @Pointcut("@annotation(com.zengdada.authority.annotation.JobSysLog)")
    public void logPointCut() {

    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint pjp) {
        return super.analysis(pjp);
    }

    @Override
    protected void setAnnotationMsg(JobSysLog annotation, SysFunkLog log) {
        log.setType(annotation.type().name());
        log.setRemark("".equals(annotation.value()) ? annotation.remark() : annotation.value());
    }

    @Override
    protected void analysisParams(Object[] params, SysFunkLog log) {
        if (null != params && params.length > 0) {
            JobExecutionContext context = (JobExecutionContext) params[0];
            JobDataMap map = context.getJobDetail().getJobDataMap();
            try {
                log.setParams(objectMapper.writeValueAsString(map));
            } catch (JsonProcessingException e) {
                log.setParams(map != null ? map.getClass().getSimpleName() : null);
            }
        } else {
            throw new RuntimeException("注解配置错误，该注解只允许配置在JOB中！");
        }
    }

}
